package com.ksu.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ksu.manager.model.TypeItem;

import com.ksu.manager.service.ITypeItemService;

/**
 * 消费类别项目controller
 * 
 * @author lambert
 * 
 */

@Controller
@RequestMapping("/typeItem")
public class TypeItemController {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(TypeItemController.class);
	private ITypeItemService typeItemService;

	public ITypeItemService getTypeItemService() {
		return typeItemService;
	}

	public void setTypeItemService(ITypeItemService typeItemService) {
		this.typeItemService = typeItemService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response,TypeItem typeItem) {
		ModelAndView mv = new ModelAndView("/typeItem/list");
		try {
			List<TypeItem> typeItemList = typeItemService.queryList(typeItem);
			mv.addObject("typeItemList", typeItemList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response,TypeItem typeItem){
		if(typeItem!=null){
		    typeItemService.add(typeItem);
		}
		return query(request,response,null);
	}
	
	@RequestMapping(value = "/del")
	public ModelAndView del(HttpServletRequest request, HttpServletResponse response,TypeItem typeItem){
		if(typeItem!=null){
		    typeItemService.del(typeItem);
		}
		return query(request,response,null);
	}
	
	@RequestMapping(value = "/mod")
	public ModelAndView mod(HttpServletRequest request, HttpServletResponse response,TypeItem typeItem){
		if(typeItem!=null){
			typeItemService.mod(typeItem);
		}
		ModelAndView mv = new ModelAndView("/login/result");
		mv.addObject("result","refreshParent");
		return mv;
	}
	
	@RequestMapping(value = "/getTypeItem")
	public ModelAndView getTypeItemById(HttpServletRequest request, HttpServletResponse response,TypeItem typeItem){
		ModelAndView mv = new ModelAndView("/typeItem/mod");
		typeItem = typeItemService.queryTypeItemById(typeItem);
		List<TypeItem> typeItemList = typeItemService.queryList(null);
		mv.addObject(typeItem);
		mv.addObject(typeItemList);
		return mv; 
	} 

}
